var express = require('express');
var router = express.Router();
var path = require('path');
const request = require('request');
// const http = require('http');
const fs = require('fs');

/* GET home page. */
router.get('/', async function(req, res, next) {
	// const file_url = 'https://txmov2.a.kwimgs.com/upic/2022/06/07/18/BMjAyMjA2MDcxODE0MDZfNzExNjg1NDMxXzc2MjUzNDc1NDMyXzJfMw==_b_Be21a1c2bf9dbbdd37f9d62edf4f8a1cf.mp4?tag=1-1657782812-std-0-5v5bwpuvb3-4c62fd1f23b5455d&clientCacheKey=3x393zsvehsfmvm_b.mp4&tt=b&di=7cdfc581&bp=11731';
	
	// const file_url = 'https://p9-sign.douyinpic.com/tos-cn-i-0813/20648ccb2706417babb981a03de7c53e~noop.webp?x-expires=1660384800&x-signature=EvEtEZ8Z4lN%2Flx%2F7FwvjhHrjA4U%3D&from=4257465056&s=PackSourceEnum_DOUYIN_REFLOW&se=false&biz_tag=aweme_images&l=202207141823580102090440402D000EA7';
	/**
	 * 下载视频
	 */
	// var fileName = `${+new Date()}.mp4`
	// var fullPath = path.resolve(path.resolve(__dirname, '1657786771215.mp4'));
	// console.log('开始下载视频：', fileName);
	// http.createServer(async(req,res)=>{
	// 	console.log(res)
	// })
	// request(encodeURI(file_url)).on('error', function (err) {
	//   console.log(error)
	// 	res.send(`出错：${err}`);
	// }).pipe(fs.createWriteStream(fullPath)).on('finish', () => {
	//   console.log('视频下载成功');
	// 	res.send('成功');
	// })
	// request(encodeURI(file_url)).on('error', function (err) {
	//   console.log(error)
	// 	res.send(`出错：${err}`);
	// }).pipe(fs.createWriteStream(fullPath)).on('finish', () => {
	//   console.log('视频下载成功');
	// 	res.send('成功');
	// })
	// const _data = await request(encodeURI(file_url),'stream');
	// res.attachment(`${+new Date()}.mp4`);
	// _data.pipe(res);
	
	// const url = 'https://v.douyin.com/e1MMESR/';
	// const p = request(encodeURI(url),(error,res)=>{
	// 	console.log('resres',res.request.href,res.request.href.match(/video\/(\S*)\/\?region/))
	// });
	// console.log(p.request)
	res.send('成功');
});

module.exports = router;
